﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Drawing.Printing;

using CodeDynamiksEngine;
using MESCodeFile;
using LiveNotificationSystem;

namespace mes_tublay
{
    public partial class frmRPFieldAssessment : Form
    {
        CodeProcessor proc = new CodeProcessor(ThreadingMode.Exclusive);
        CodeScript script;

        void proc_ProcessCompleted(object sender, EventArgs e)
        {
            proc.SafeCall(this, delegate
            {
                if (proc.State == ProcessorFlag.Error ||
                    proc.State == ProcessorFlag.DataRdyErr ||
                    proc.State == ProcessorFlag.ReadyErr)
                {
                    MessageBox.Show(proc.GetProcessorMessage, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    proc.ClearErrorFlag();
                    return;
                }
               switch (proc.GetLastProcessName().ToString())
                {
                    case "SAVEAPPRAISAL_LAND":
                       /* proc.DataFetch(out dat);
                        //Set the active page before exporting
                        if (dat.GetPageSize() >= 1)
                        {
                            dat.SetActivePage(0);
                            grdBusinessPermit.DataSource = dat.ExportPage();

                        } */
                        MessageBox.Show("Field Appraisal for Land - Saved", "Saved!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        break;
                }  
            });
        }

        public frmRPFieldAssessment()
        {
            InitializeComponent();
            InitializeFrameSliderForm();
        }
        private void InitializeForm()
        {
            frmRPFieldAssessment_Load();
            Land.Click += new EventHandler(Land_Click);
            Building.Click += new EventHandler(Building_Click);
            Machinery.Click+=new EventHandler(Machinery_Click);
            
            btnSave.Click += new EventHandler(btnSave_Click);
            btnNew.Click += new EventHandler(btnNew_Click);
            proc.ProcessCompleted +=new EventHandler(proc_ProcessCompleted);

            //ControlTab.Enable_Tab(tabRPFieldAppraisalLand.TabPages[1], false);

        }

        
        void Land_Click(object sender, EventArgs e)
        {
            label37.Visible = false;
            txtLandOwner.Visible = false;
            label38.Visible = false;
            txtLandOwnerPIN.Visible = false;
            label39.Visible = false;
            txtBldgOwnerPIN.Visible = false;
            label40.Visible = false;
            txtBldgOwner.Visible = false;

        }

        void Building_Click(object sender, EventArgs e)
        {

        }

        void Machinery_Click(object sender, EventArgs e)
        {

        }

        void btnNew_Click(object sender, EventArgs e)
        {
            btnSave.Enabled = true;
        }
        void btnSave_Click(object sender, EventArgs e)
        {
            String transCode, ARPNo, PIN, OCT, OCT_dated, surveyNo, lotNo, blockNo;
            String ownerLastName, ownerFirstName, ownerMiddle, ownerTIN, ownerAddress, ownerTelNo;
            String admBUserLName, admBUserFName, admBUserMName;

            transCode = txtTransactionCode.Text;
            ARPNo = txtARPNo.Text;
            PIN = txtPIN.Text;
            OCT = txtOCT_TCT_CLOA.Text;
            OCT_dated = txtOCT_date.Value.ToString("yyyy-MM-dd");
            surveyNo = txtSurveyNo.Text;
            lotNo = txtLotNo.Text;
            blockNo = txtBlockNo.Text;

            ownerLastName = txtOwnerLName.Text;
            ownerFirstName = txtOwnerFName.Text;
            ownerMiddle = txtOwnerMName.Text;
            ownerTIN = txtOwnerTIN.Text;
            ownerAddress = txtOwnerAddress.Text;
            ownerTelNo = txtOwnerTelNo.Text;
            admBUserLName = txtAdminBUserLName.Text;
            admBUserFName = txtAdminBUserFName.Text;
            admBUserMName = txtAdminBUserMName.Text;

            DialogResult submit = MessageBox.Show("Are all information complete and correct?", "Confirm Submission", MessageBoxButtons.YesNo);
            if (submit == DialogResult.Yes)
            {
                if (proc.State != ProcessorFlag.Ready && proc.State != ProcessorFlag.DataRdy) return;
                //Instantiate the script object and specify a name for the script.
                script = new CodeScript("SAVEAPPRAISAL_LAND");
                script.Add(CodeIns.SYS_RESET);
                script.Add(CodeIns.DBU_NEW_CORE);
                script.Add(CodeIns.DBU_CONNECT, Common.CFG.Read("DRIVER"), Common.CFG.Read("DB"), Common.CFG.Read("UID"), Common.CFG.Read("PWD"), Common.CFG.Read("SERVER"));
                script.Add(CodeIns.DBU_BEGIN_TRANSACTION);
                script.Add(CodeIns.DBU_INSERT, "tblrpfieldappraisalland",
                    new string[] { "fTransactionCode", "fARPNo", "fPropertyIDNo", "fOCT_TCT_CLOA_No", "fOCT_TCT_CLOA_Date",
                        "fSurveyNo","fBlockNo","fLotNo","fOwnerLastName","fOwnerFirstName","fOwnerMiddleName","fOwnerTIN","fOwnerAddress",
                        "fOwnerTelNo","fAdminLastName","fAdminFirstName","fAdminMiddleName" },
                    new object[] {"'"+transCode+"'","'"+ARPNo+"'","'"+PIN+"'","'"+OCT+"'","'"+OCT_dated+"'","'"+surveyNo+"'",
                        "'"+lotNo+"'","'"+blockNo+"'","'"+ownerLastName+"'","'"+ownerFirstName+"'","'"+ownerMiddle+"'","'"+ownerTIN+"'",
                        "'"+ownerAddress+"'","'"+ownerTelNo+"'","'"+admBUserLName+"'","'"+admBUserFName+"'","'"+admBUserMName+"'"});

               script.Add(CodeIns.DBU_END);
                //After Constructing the script, pass it to the processor.
                proc.Run(script);
                //Common.NOTIFY.Push("TREASURY_DEPT_TREASURER", "New Business Permit Application: " + lblApplicationNumber.Text + "- For Approval ", new string[] { "" + lblApplicationNumber.Text });

               
            }
            else
            {
                MessageBox.Show("Field Appraisal for Land is not yet saved.", "Save Failed!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
    
    

            

        private void frmRPFieldAssessment_Load()
        {
            btnSave.Enabled = false;
            btnPrint.Enabled = false;

        }
 
    }
}
